<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- /导航栏 -->
    <!--
通过v-model绑定当前激活标签对应的索引值，默认情况下启用第一个标签
通过animated属性可以开启切换标签内容时的转场动画
通过swipeable属性可以开启滑动切换标签页
-->
    <van-tabs
      class="channel-tabs"
      v-model="active"
      animated
      swipeable
      swipe-threshold
    >
      <van-tab
        v-for="channel in channels"
        :title="channel.name"
        :key="channel.id"
      >
        <ArticleList :channel="channel"></ArticleList>
      </van-tab>
      <div slot="nav-right" class="placeholder"></div>
      <div
        slot="nav-right"
        class="hamburger-btn"
        @click="isChannelEditShow = true"
      >
        <!-- <div class="border-left"></div> -->
        <i class="toutiao toutiao-gengduo"></i>
      </div>
    </van-tabs>
    <!--  -->
    <!-- 频道编辑 -->
    <van-popup
      class="edit-channel-popup"
      v-model="isChannelEditShow"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-right"
    >
      <ChannelEdit
        :my-channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      >
        <!-- 上面可以写v-model=active ,上面的子组件channel的active,换成value，下面的单击方法的时间好像~可以变成value  -->
      </ChannelEdit>
    </van-popup>
    <!-- /频道编辑 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user'
import ArticleList from './components/ArticleList.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage'
export default {
  name: 'HomeIndex',
  components: {
    ArticleList,
    ChannelEdit
  },
  props: {},
  data () {
    return {
      active: 0,
      channels: [], // 频道列表
      isChannelEditShow: false // 这里我们先设置为 true 就能看到弹窗的页面了
    }
  },
  computed: {
    ...mapState([['user']])
  },
  watch: {},
  created () {
    // 3. 调用获取频道列表
    this.loadChannels()
  },
  mounted () {},
  methods: {
    async loadChannels () {
      try {
        /*  const { data } = await getUserChannels();
        this.channels = data.data.channels;
        console.log(data); */
        /*
        1.先判断用户是否登录
        2.如果已经登录，直接获取用户的频道列表
        2.1判断本地存储是否有数据，如果有就拿来用
        2.2如果本地存储也没有数据，调用后台推荐频道列表
        */
        let channels = []
        if (this.user) {
          const { data } = await getUserChannels()
          channels = data.data.channels
        } else {
          const localChannels = getItem('TOUTIAO_CHANNELS')
          if (localChannels) {
            channels = localChannels
          } else {
            const { data } = await getUserChannels()
            channels = data.data.channels
          }
        }
        this.channels = channels
        /*
        老师的操作
        if (this.user) {
          1.如果用户已经登录，直接获取用户的频道列表
          const { data } = await getUserChannels();
          this.channels = data.data.channels;
        } else {
          2.如果未登录
          const localChannels = getItem("TOUTIAO_CHANNELS");
          if (localChannels) {
          2.1判断本地存储是否有数据，如果有就拿来用
            this.channels = localChannels;
          } else {
          2.2如果本地存储也没有数据，调用后台推荐频道列表
            const { data } = await getUserChannels();
            this.channels = data.data.channels;
          }
        }
        */
      } catch (err) {
        this.$toast('获取频道列表数据失败')
      }
    },
    /*
    async loadChannels(){
      const {data}=await getUserChannels();
      console.log
    }
    */
    onUpdateActive (index, flag = true) {
      console.log('home', index)
      /* 更新 */
      this.active = index
      /* 关闭 */
      this.isChannelEditShow = flag
    }
  }
}
</script>

<style scoped lang="less">
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  .van-nav-bar__title {
    max-width: unset;
  }
  // 搜索样式
  .search-btn {
    width: 555px;
    height: 64px;
    background-image: linear-gradient(
      43deg,
      #4158d0 0%,
      #c850c0 46%,
      #ffcc70 100%
    );
    border: none;
    font-size: 30px;
    font-weight: 600;
    .van-icon {
      font-size: 35px;
      font-weight: 600;
    }
  }
  /deep/ .channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      height: 6px;
      background-color: #e7eaee;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-image: linear-gradient(
        45deg,
        #fa8bff 0%,
        #2bd2ff 52%,
        #2bff88 90%
      );
      border-radius: 12px;

      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: "";
        position: absolute;
        left: 0;
        width: 1px;
        height: 58px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
.page-nav-bar {
  box-shadow: 0 0.06667rem 0.33333rem 0.13333rem #fff;
}
/*     老师的标签渐变代替图片方法
    .hamburger-btn {
    flex-shrink: 0;
    width: 66px;
    height: 82px;
    position: fixed;
    right: 0;
    text-align: center;
    line-height: 82px;
    background-color: #fff;
    opacity: 0.902;
    i.toutiao{
      font-size: 33px;
    }
    .border-left{
      height: 100%;
      position: absolute;
      left: 0;
      background-image: linear-gradient(transparent,#dfdfdf 25%,#e6e6e6 50%,#dfdfdf 75%,transparent 100%);
    }
    }*/
</style>
